home *** CD-ROM | disk | FTP | other *** search
- Path: news.primenet.com!not-for-mail
- From: gbe@primenet.com (Gary Edstrom)
- Newsgroups: comp.lang.c++
- Subject: Re: HELP!!! - loop control problems
- Date: 20 Jan 1996 19:57:01 -0700
- Organization: Sequoia Software
- Sender: root@primenet.com
- Message-ID: <3101abf0.164809733@news.primenet.com>
- References: <4ds174$6t0@bolivia.it.earthlink.net>
- X-Posted-By: ip008.lax.primenet.com
- X-Newsreader: Forte Agent .99c/16.141
-
- macc@earthlink.net (OB1) wrote:
-
- >I have the following code in a program I'm writing:
- >
- >const long SCREEN_SIZE = 320 * 200;
- >long count = 0;
- >while (count <= SCREEN_SIZE)
- >{
- > ... code (count gets incremented)
- >}
- >
- >When I trace through my program, I find that the loop end condition is
- >always true and the code inside the loop is never run. I looking for
- >any ideas on why this doesn't work.
-
- If you are using a 16 bit compiler, the product of 320 * 200 is 64,000
- which is interpreted as a negative number in the world of 16 bit
- integers. It remains negative when it is sign extended to a 32 bit
- number. As a result, count is never less than screen size.
-
- Try this:
-
- const long SCREEN_SIZE = (long) 320 * 200;
-
- I believe you'll find that it works.
-
- Gary
-
- --
- Gary Edstrom <gbe@primenet.com> | Sequoia Software
- PO Box 9573 | Programming & Technical Services
- Glendale CA 91226-0573 | PGP Key ID: 0x1A0D44BD
- PGP Fingerprint: 72 AA 4F 73 05 53 89 C6 8A EE F4 EE D1 C0 13 8D
-